<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="机加工序" prop="dictMachiningProcess">
        <el-select
          v-model="queryParams.dictMachiningProcess"
          placeholder="请选择机加工序"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MACHINING_PROCESS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="零件名称" prop="partsName">
        <el-input
          v-model="queryParams.partsName"
          placeholder="请输入零件名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="规格" prop="specification">
        <el-input
          v-model="queryParams.specification"
          placeholder="请输入规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="加工工序" prop="processing">
        <el-input
          v-model="queryParams.processing"
          placeholder="请输入加工工序"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="setSearchParams(queryParams)">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
      <div  style="width: 100%; height: 50px; display: flex; justify-content: space-between">
        <div>
      <el-button
        type="primary"
        @click="showImport"
        v-hasPermi="['config:wage-machining-unit-price:import']"
      >
      数据导入
      </el-button>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['config:wage-machining-unit-price:export']"
        >
        数据导出
        </el-button>
          <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['config:wage-machining-unit-price:create']"
        >
        新增
        </el-button>
        </div>
        <div>
          <el-button
            type="primary"
            @click="openTableTitleSet"
          >
          表头设置
          </el-button>
        </div>
      </div>
      <!-- 表单 -->
      <Table
        :columns="tableTitleList"
        :selection="tableObject.tableList.length ? true : false"
        @register="tableoperation"
        :data="tableObject.tableList"
        :loading="tableObject.loading"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :tableHeight="tableHeight"
      >
        <template #action="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            v-hasPermi="['config:wage-machining-unit-price:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            v-hasPermi="['config:wage-machining-unit-price:delete']"
          >
            删除
          </el-button>
        </template>
      </Table>
      </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WageMachiningUnitPriceForm ref="formRef" @success="getList" />
      <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
    <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
    <!-- 上传文件 -->
  <CusUploadFile ref="cusUploadFile" @refresh="getList"/>
</template>

<script setup name="WageMachiningUnitPrice">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as WageMachiningUnitPriceApi from '@/api/config/wageMachiningUnitPrice'
import WageMachiningUnitPriceForm from './WageMachiningUnitPriceForm.vue'
let  tableTitleList = ref([]) // 表头的数据
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
// const total = ref(0) // 列表的总页数
// const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dictMachiningProcess: null,
  partsName: null,
  specification: null,
  processing: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/**表头数据 */
const getLabelAll = async () => {
  loading.value = true
  try {
    const res = await WageMachiningUnitPriceApi.getWageMachiningUnitPricePage(queryParams)
    res.labels.push({
      label: '操作',
        field: 'action',
        isDetail: false,
        width: 110
    })
    const {allSchemas} = useCrudSchemas(res.labels)
    tableTitleList.value = allSchemas.tableColumns;
    /**表头修改需要刷新表格 */
    getList()
  } finally {
    loading.value = false
  }
}

 /**打开表格表头设置**/
 const tableTitleSet = ref() 
const openTableTitleSet = () => {
  const arr = tableTitleList.value.slice(0,-1)
  let code = 'WageMachiningUnitPrice'
  tableTitleSet.value.open(arr, code)
}

/**导入显示 */
const cusUploadFile = ref()
const showImport = () => {
  cusUploadFile.value.open(
    '/config/wage-machining-unit-price/get-import-template',
    '/config/wage-machining-unit-price/import'
  )
}

/**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (parent) => {
  usTableoperation.value = parent
}

/** 查询列表 */
const { tableObject, tableMethods } = useTable({
    getListApi:WageMachiningUnitPriceApi.getWageMachiningUnitPricePage, // 分页接口
    defaultParams: queryParams
  })

  /** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type, id) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WageMachiningUnitPriceApi.deleteWageMachiningUnitPrice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

   //清空多选
   const clearSelectData = () => {
     usTableoperation.value.selections = []
     usTableoperation.value.clearTableSelect()
   }


// 导出数据
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = []
  } else {
    params.ids = usTableoperation.value.selections.map((item) => item.id)
  }
  WageMachiningUnitPriceApi.exportWageMachiningUnitPrice(params).then((res) => {
    download.excel(res, val.name)
    clearSelectData()
  })
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted( async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList() 
})
</script>

  
